package com.kll.tv.views.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.kll.carolinehmibiz.net.bean.GetSystemMenuDetailResponse
import com.kll.tv.R
import com.kll.tv.databinding.LayoutHorArticleHeadBinding
import com.kll.tv.views.ImageReviewActivity
import com.kll.tv.views.ui.listener.IArticleFramLayout

class HArticleFramLayout: ConstraintLayout, IArticleFramLayout, View.OnClickListener {
    lateinit var binding: LayoutHorArticleHeadBinding
    private var data: ArrayList<GetSystemMenuDetailResponse>? = null

    constructor(context: Context): super(context) {
        initLayout()
    }
    constructor(context: Context, attrs: AttributeSet?) :this(context, attrs, 0) {
        initLayout()
    }
    constructor(
        context: Context, attrs: AttributeSet?,
        defStyleAttr: Int
    ): super(context, attrs, defStyleAttr){
        initLayout()
    }

    override fun initLayout() {
        LayoutInflater.from(context).inflate(R.layout.layout_hor_article_head, this)
        binding = LayoutHorArticleHeadBinding.bind(this)
        binding.imageView1.setOnClickListener(this)
        binding.imageView2.setOnClickListener(this)
        binding.imageView3.setOnClickListener(this)
        binding.imageView4.setOnClickListener(this)
        binding.imageView5.setOnClickListener(this)
    }


    override fun refreshUI(menuDetail: ArrayList<GetSystemMenuDetailResponse>) {
        data = menuDetail
        Glide.with(this).load(menuDetail[0].content_1!![0].url).into(binding.imageView1)
        Glide.with(this).load(menuDetail[0].content_1!![1].url).into(binding.imageView2)
        Glide.with(this).load(menuDetail[0].content_1!![2].url).into(binding.imageView3)
        Glide.with(this).load(menuDetail[0].content_1!![3].url).into(binding.imageView4)
        Glide.with(this).load(menuDetail[0].content_1!![4].url).into(binding.imageView5)
    }

    override fun onClick(v: View?) {
        when(v!!.id) {
            R.id.imageView1 -> toImageReview(0)
            R.id.imageView2 -> toImageReview(1)
            R.id.imageView3 -> toImageReview(2)
            R.id.imageView4 -> toImageReview(3)
            R.id.imageView5 -> toImageReview(4)
        }
    }

    private fun toImageReview(position: Int) {
        val intent = Intent(context, ImageReviewActivity::class.java)
        val bundle = Bundle()
        val photos = getSpecialResult(data?.get(0)?.content_1!!)
        bundle.putSerializable("data", photos)
        intent.putExtra("data", bundle)
        intent.putExtra("index", position)
        context.startActivity(intent)
    }

    /**
     * 过滤arraylist
     */
    private fun getSpecialResult(arrays: ArrayList<GetSystemMenuDetailResponse.ImageInfo>): ArrayList<String> {
        val resultArrays = ArrayList<String>()
        for (special in arrays) {
            if (special.type == 1) {
                resultArrays.add(special.url)
            }
        }
        return resultArrays
    }

}